package cn.jsprun.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class LogLucene {
	@SuppressWarnings("unchecked")
	public static List readLog(FileInputStream filereader,String resultWord) {
		List list = new ArrayList();
		BufferedReader br = null;
		InputStreamReader ir=null;
		try {
			ir = new InputStreamReader(filereader,JspRunConfig.charset);
			br = new BufferedReader(ir);
			String newline = br.readLine();
			while (newline != null) {
				String []str = newline.split("\\s");
				for(int i=0;i<str.length;i++){
					if(str[i].indexOf(resultWord)!=-1){
						list.add(newline);
						break;
					}
				}
				newline = br.readLine();
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {				
					br.close();	
					ir.close();
					if (filereader != null) {
						filereader.close();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	public static List readRatelog(String path,String resultWord) {
		List list = new ArrayList();
		Calendar calendar=Common.getGMTCalendar();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_ratelog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);
				List nowtime = readLog(fr,resultWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_ratelog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);
				List uptime = readLog(fr,resultWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	public static List readMedalsLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_medalslog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_medalslog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List readBanLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_banlog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_banlog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List readIllegalLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_illegallog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);  
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_illegallog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);  
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List readCpLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_cplog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);  
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_cplog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);  
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List readModsLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_modslog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);  
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_modslog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);  
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List readErrorLog(String path,String keyWord) {
		List list = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		String mouth = "";
		int calemouth = calendar.get(Calendar.MONTH) + 1;
		if (calemouth < 10) {
			mouth = "0" + calemouth;
		} else {
			mouth = "" + calemouth;
		}
		String nowpath = path + "/" + calendar.get(Calendar.YEAR) + mouth
				+ "_errorlog.jsp";
		File f = new File(nowpath);
		if (f.exists()) {
			try {
				FileInputStream fr = new FileInputStream(nowpath);  
				List nowtime = readLog(fr,keyWord);
				list.add(nowtime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		String mouthup = "";
		int calemouthup = calendar.get(Calendar.MONTH);
		if (calemouthup < 10) {
			mouthup = "0" + calemouthup;
		} else {
			mouthup = "" + calemouthup;
		}
		String uppath = path + "/" + calendar.get(Calendar.YEAR) + mouthup
				+ "_errorlog.jsp";
		File upf = new File(uppath);
		if (upf.exists()) {
			try {
				FileInputStream fr = new FileInputStream(uppath);  
				List uptime = readLog(fr,keyWord);
				list.add(uptime);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

}
